﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Jzy.Core.Utility;
using Jzy.Core.Dict;
using ECommerce.Common;
using Jzy.Core.Common;
using ECommerce.Model.Product;
using ECommerce.BLL.Product;
using Jzy.Core.Common.Web;
using Jzy.Core.Attach;
using Jzy.Core.Common.Basic;

namespace ECommerce.Web.ECAdmin.Product
{
    public partial class Edit : PageEdit
    {
        private ECProductBO Product
        {
            get
            {
                return ECProductBO.Instance<ECProductBO>();
            }
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            JSUtil.SetDivHeight(pnlMain.ClientID, 32);
            if (!IsPostBack)
            {
                trImages.Visible = false;

                BindType();
                if (!string.IsNullOrEmpty(this.PKID))
                {
                    DispInfo();
                }
            }
            AddAttr();
        }

        private void AddAttr()
        {
            JSUtil.ClickBtn(lnkSave, btnSave);
        }

        private void BindType()
        {
            DictBO dict = new DictBO();
            dict.BindListControl(Type1, (int)DictItem.品牌分类);

            dict.BindListControl(Type2, (int)DictItem.用途分类);
            dict.BindListControl(Type3, (int)DictItem.材料分类);
            dict.BindListControl(Type4, (int)DictItem.对象分类);
            dict.BindListControl(Type5, (int)DictItem.造型分类);

            IEnumerable<DictInfo> list = dict.GetList((int)DictItem.蛋糕尺寸);
            rptSize.DataSource = list;
            rptSize.DataBind();
           

            List<EnumInfo> states = ConvertUtil.ConvertEnum(typeof(ProductState));
            State.DataSource = states;
            State.DataTextField = "Name";
            State.DataValueField = "ID";
            State.DataBind();

        }

        private void DispInfo()
        {
            ECProductInfo info = this.Product.GetInfo(this.PKID);
            EntityUtil.SetControl(pnlMain, info);

            if (!string.IsNullOrEmpty(info.SmallImage))
            {
                imgSmallImage.Visible = true;
                imgSmallImage.ImageUrl = info.SmallImageAbsPath;
                lnkSmallImage.HRef = info.AdImageAbsPath;
                btnDelete.Visible = true;
            }
            else
            {
                imgSmallImage.Visible = false;
                btnDelete.Visible = false;
            }

            if (!string.IsNullOrEmpty(info.AdImage))
            {
                imgAdImage.Visible = true;
                imgAdImage.ImageUrl = info.AdImageAbsPath;
                lnkAdImage.HRef = info.AdImageAbsPath;
                btnDelete2.Visible = true;
            }
            else
            {
                imgAdImage.Visible = false;
                btnDelete2.Visible = false;
            }

            rptPicture.DataSource = info.Pictures;
            rptPicture.DataBind();
            if (info.Pictures.Count > 0)
            {
                trImages.Visible = true;
            }
            else
            {
                trImages.Visible = false;
            }

            if (info.Size != null)
            {
                string[] lstSize = info.Size.Split(',');
                foreach (RepeaterItem item in rptSize.Items)
                {
                    if (item.ItemType == ListItemType.Item || item.ItemType == ListItemType.AlternatingItem)
                    {
                        CheckBox chkSize = (CheckBox)item.FindControl("chkSize");
                        HiddenField hidID = (HiddenField)item.FindControl("hidID");
                        if (lstSize.Contains(hidID.Value))
                        {
                            chkSize.Checked = true;
                        }
                    }
                }
            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            ECProductInfo info = Product.GetInfo(PKID);

            EntityUtil.SetEntity(pnlMain, info);
            if (string.IsNullOrEmpty(PKID))
            {
                info.InputTime = DateTime.Now;               
            }
            info.CreatorID = Jzy.Common.AppSession.UserID;
            info.CreatorName = Jzy.Common.AppSession.UserName;
            if (fileSmallImage.FileName != "")
            {
                if (!string.IsNullOrEmpty(info.SmallImage))
                {
                    this.Product.DeleteSmallImage(info.ID);
                }
                info.SmallImage = this.Product.SaveImage(fileSmallImage.PostedFile,254);
            }

            if (fileAdImage.FileName != "")
            {
                if (!string.IsNullOrEmpty(info.AdImage))
                {
                    this.Product.DeleteAdImage(info.ID);
                }
                info.AdImage = this.Product.SaveImage(fileAdImage.PostedFile,794);
            }

            info.Size = ",";
            foreach (RepeaterItem item in rptSize.Items)
            {
                if (item.ItemType == ListItemType.Item || item.ItemType == ListItemType.AlternatingItem)
                {
                    CheckBox chkSize = (CheckBox)item.FindControl("chkSize");
                    HiddenField hidID = (HiddenField)item.FindControl("hidID");
                    if (chkSize.Checked == true)
                    {
                        info.Size += hidID.Value + ",";
                    }

                }
            }

            info.Size = info.Size.Trim(',');

            this.Product.Save(info);

            UserImage userImage = new UserImage("ECProductPicture");
            ImageBO picture = new ImageBO("ECProductPicture");
            foreach (string key in Request.Files.Keys)
            {
                if (key.Contains("pic"))
                {
                    //判断图片格式
                    string extention = System.IO.Path.GetExtension(Request.Files[key].FileName).ToLower();
                    if (extention == ".jpg" || extention == ".png" || extention == ".gif" || extention == ".bmp")
                    {
                        System.Drawing.Bitmap img = new System.Drawing.Bitmap(Request.Files[key].InputStream);
                        ImageInfo pictureInfo = new ImageInfo();
                        pictureInfo.Name = System.IO.Path.GetFileName(Request.Files[key].FileName);
                        pictureInfo.BindID = info.ID;
                        pictureInfo.InputTime = DateTime.Now;
                        pictureInfo.Width = img.Width;
                        pictureInfo.Height = img.Height;
                        pictureInfo.Size = (int)Request.Files[key].InputStream.Length;
                        pictureInfo.Orders = picture.GetMaxOrder(info.ID);

                        pictureInfo.SmallImage = userImage.Save(Request.Files[key],88);
                        pictureInfo.BigImage = userImage.Save(Request.Files[key],422);
                        pictureInfo.OriginImage = userImage.Save(Request.Files[key],800);                      

                        picture.Save(pictureInfo);
                    }
                }
            }
            string script = "alert('保存成功');location.href='List.aspx';";
            JSUtil.ResponseJS(script);

            

        }

        protected void btnDelete_Click(object sender, ImageClickEventArgs e)
        {
            ECProductInfo info = this.Product.GetInfo(this.PKID);
            this.Product.DeleteSmallImage(info.ID);
            info.SmallImage = null;
            Product.Save(info);
            DispInfo();
        }

        protected void btnDelete2_Click(object sender, ImageClickEventArgs e)
        {
            ECProductInfo info = this.Product.GetInfo(this.PKID);
            this.Product.DeleteAdImage(info.ID);
            info.AdImage = null;
            Product.Save(info);
            DispInfo();
        }

        protected void rptPicture_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            ImageBO picture = new ImageBO("ECProductPicture");
            picture.Delete(e.CommandArgument);
            DispInfo();
        }

        protected void rptSize_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {

            }
        }
    }
}